package com.talkyun.openx.server;

import javax.servlet.ServletContext;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ServletStatus {
    private final Date upTime;

    public ServletStatus(ServletContext sc) {
        this.upTime = new Date();
    }

    public String getStatus(String format) {
        // TODO IMPL
        // os.jdk.servlet.version : yyyyMMdd HH:mm:ss
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return "OK ( up " + getUpTimeString() + " | " + fmt.format(upTime) + " ~ " + fmt.format(new Date()) + " )";
    }

    private String getUpTimeString() {
        long time = (System.currentTimeMillis() - upTime.getTime()) / 1000;
        long seconds = time % 60;
        long minutes = time / 60;
        long hours = time / 60 / 60;
        long days = time / 60 / 60 / 24;
        return (days == 0 ? "" : (days + "天")) + (hours == 0 ? "" : (hours % 24 + "小时")) + (minutes == 0 ? "" : (minutes % 60 + "分钟")) + (seconds == 0 ? "" : (seconds + "秒"));
    }

    public static void main(String[] args) {

    }
}
